package com.cn.kabalifeuser.ui.my;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.RequiresApi;

import com.cn.kabalifeuser.R;
import com.cn.kabalifeuser.base.LazyFragment;
import com.cn.kabalifeuser.config.CustomRefreshListener;
import com.cn.kabalifeuser.config.LoginHelper;
import com.cn.kabalifeuser.config.RefreshHelper;
import com.cn.kabalifeuser.ui.my.activity.CardrollActivity;
import com.cn.kabalifeuser.ui.my.activity.CouponActivity;
import com.cn.kabalifeuser.ui.my.activity.CooperationActivity;
import com.cn.kabalifeuser.ui.my.activity.FansListAcivity;
import com.cn.kabalifeuser.ui.my.activity.HelpListActivity;
import com.cn.kabalifeuser.ui.my.activity.IntegralListActivity;
import com.cn.kabalifeuser.ui.my.activity.IntegralRuleActivity;
import com.cn.kabalifeuser.ui.my.activity.InviteFriendsActivtiy;
import com.cn.kabalifeuser.ui.my.activity.MyAccountActivity;
import com.cn.kabalifeuser.ui.my.activity.NwsMsgActivity;
import com.cn.kabalifeuser.ui.my.activity.OpinionActivity;
import com.cn.kabalifeuser.ui.my.activity.OrderListAcivity;
import com.cn.kabalifeuser.ui.my.activity.ReportFormAcivity;
import com.cn.kabalifeuser.ui.my.activity.UserInfoActivity;
import com.cn.kabalifeuser.ui.my.activity.VipActivity;
import com.cn.kabalifeuser.ui.my.bean.MyUserInfoBean;
import com.cn.kabalifeuser.ui.service.bean.BannerListBean;
import com.cn.kabalifeuser.utils.GlideUtil;
import com.cn.kabalifeuser.utils.RxToast;
import com.cn.kabalifeuser.view.FlowLayoutManager1;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;

import static android.content.Context.CLIPBOARD_SERVICE;

/**
 * Created by 晓林 on 2020/9/2.
 * function:
 */
public class FragmentMy extends LazyFragment<FragmentMyView, FragmentMyPresenter> implements FragmentMyView {
    @BindView(R.id.tv_user_name)
    TextView tvUserName;
    @BindView(R.id.tv_user_stock)
    TextView tvUserStock;
    @BindView(R.id.tv_user_code)
    TextView tvUserCode;
    @BindView(R.id.tv_num_cardroll)
    TextView tvNumCardroll;
    @BindView(R.id.tv_num_fans)
    TextView tvNumFans;
    @BindView(R.id.tv_num_profit)
    TextView tvNumProfit;
    @BindView(R.id.img_user_head)
    CircleImageView imgUserHead;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.tv_user_phone)
    TextView tvUserPhone;
    @BindView(R.id.tv_msg)
    TextView tvMsg;
    @BindView(R.id.img_btn_vip)
    ImageView img;

    MyUserInfoBean myUserInfoBean;

    @Override
    protected FragmentMyPresenter createPresenter() {
        return new FragmentMyPresenter();
    }

    @Override
    protected void onUserVisible() {

    }

    @Override
    protected int provideContentViewId() {
        return R.layout.fragment_home_my;
    }

    @Override
    public void onResume() {
        super.onResume();
        mPresenter.onUserInfoData(LoginHelper.getLonginData().getResult());
    }

    public void initRefreshLayout() {
        RefreshHelper.initRefreshLayout(refreshLayout, new CustomRefreshListener() {
            @Override
            public void _onRefresh(RefreshLayout refreshLayout) {
                mPresenter.onUserInfoData(LoginHelper.getLonginData().getResult());
            }

            @Override
            public void _onLoadMore(RefreshLayout refreshLayout) {

            }
        });
    }

    @Override
    public void initView(View view) {
        super.initView(view);
        ButterKnife.bind(this, view);
        initRefreshLayout();
        mPresenter.onBannerData(LoginHelper.getLonginData().getResult(), 5);
    }

    /**
     * 复制
     *
     * @param context
     * @param copyField
     */

    public static void CopyToClipboard(Context context, String copyField) {
        ClipData myClip = ClipData.newPlainText("text", copyField);
        ClipboardManager myClipboard = (ClipboardManager) context.getSystemService(CLIPBOARD_SERVICE);
        myClipboard.setPrimaryClip(myClip);
//            Toast.makeText(context, "复制成功", Toast.LENGTH_LONG).show();
        RxToast.success("复制成功");
    }

    @OnClick({R.id.tv_btn_my_integral_list, R.id.img_btn_vip, R.id.ll_btn_set, R.id.ll_btn_nws, R.id.tv_copy, R.id.ll_btn_cardroll, R.id.ll_btn_fans, R.id.ll_btn_profit, R.id.tv_btn_order_service, R.id.tv_btn_order_Integral, R.id.tv_btn_order_report_form, R.id.tv_btn_order_invite_friends, R.id.tv_btn_my_fans, R.id.tv_btn_my_cardroll, R.id.tv_btn_my_coupon, R.id.tv_btn_my_account, R.id.tv_btn_my_cooperation, R.id.tv_btn_my_integral, R.id.tv_btn_my_help, R.id.tv_btn_my_opinion, R.id.tv_btn_my_phone})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_btn_vip:
                //TODO VIP
                if (myUserInfoBean != null) {
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("bean", myUserInfoBean);
                    $startActivity(VipActivity.class, bundle);
                }
                break;
            case R.id.ll_btn_set:
                //todo 设置
                $startActivity(UserInfoActivity.class);
                break;
            case R.id.ll_btn_nws:
                //todo 消息
                $startActivity(NwsMsgActivity.class);
                break;
            case R.id.tv_copy:
                //todo 复制
                if (myUserInfoBean != null)
                    CopyToClipboard(getMe(), myUserInfoBean.getResult().getInvite());
                break;
            case R.id.ll_btn_cardroll:
                //todo 卡卷
//                RxToast.success("卡卷");
                break;
            case R.id.ll_btn_fans:
                //todo 粉丝
//                RxToast.success("粉丝");
                break;
            case R.id.ll_btn_profit:
                //todo 收益
//                RxToast.success("收益");
                break;
            case R.id.tv_btn_order_service:
                //todo 服务订单
                Bundle bundle = new Bundle();
                bundle.putInt("sign", 1);
                $startActivity(OrderListAcivity.class, bundle);
                break;
            case R.id.tv_btn_order_Integral:
                //todo 积分订单
                bundle = new Bundle();
                bundle.putInt("sign", 2);
                $startActivity(OrderListAcivity.class, bundle);
                break;
            case R.id.tv_btn_order_report_form:
                //todo 报表订单
                $startActivity(ReportFormAcivity.class);
                break;
            case R.id.tv_btn_order_invite_friends:
                //todo 邀请好友
                if (myUserInfoBean != null) {
                    bundle = new Bundle();
                    bundle.putString("Invite", myUserInfoBean.getResult().getInvite());
                    bundle.putString("Head", myUserInfoBean.getResult().getHead());
                    bundle.putString("Nickname", myUserInfoBean.getResult().getNickname());
                    $startActivity(InviteFriendsActivtiy.class, bundle);
                }
                break;
            case R.id.tv_btn_my_fans:
                //todo 我的粉丝
                $startActivity(FansListAcivity.class);
                break;
            case R.id.tv_btn_my_cardroll:
                //todo 我的卡卷
                $startActivity(CardrollActivity.class);
                break;
            case R.id.tv_btn_my_coupon:
                //todo 领劵中心
                $startActivity(CouponActivity.class);
                break;
            case R.id.tv_btn_my_account:
                //todo 账户
                $startActivity(MyAccountActivity.class);
                break;
            case R.id.tv_btn_my_cooperation:
                //todo 合作
                $startActivity(CooperationActivity.class);
                break;
            case R.id.tv_btn_my_integral:
                //todo 积分规则
                $startActivity(IntegralRuleActivity.class);
                break;
            case R.id.tv_btn_my_integral_list:
                bundle = new Bundle();
                bundle.putInt("integral", myUserInfoBean.getResult().getIntegral());
                $startActivity(IntegralListActivity.class, bundle);
                break;
            case R.id.tv_btn_my_help:
                //todo 帮助
                $startActivity(HelpListActivity.class);
                break;
            case R.id.tv_btn_my_opinion:
                //todo 意见
                $startActivity(OpinionActivity.class);
                break;
            case R.id.tv_btn_my_phone:
                //todo 客服
                if (myUserInfoBean != null) {
                    Intent intent = new Intent(Intent.ACTION_DIAL);
                    Uri data = Uri.parse("tel:" + myUserInfoBean.getResult().getTel());
                    intent.setData(data);
                    startActivity(intent);
                }
                break;
        }
    }

    @Override
    public void onReLoggedIn(String msg) {
        RxToast.error(msg);
        LoginHelper.againGoToLoginActivity(getMe());
    }

    @Override
    public void onError(String msg) {
        RxToast.error(msg);
    }

    @Override
    public Context _getContext() {
        return getMe();
    }

    @Override
    public void onBannerSuccess(BannerListBean bean) {
        if (bean.getResult().size()>0)
            GlideUtil.ImageLoad(getMe(),bean.getResult().get(0).getBanner(),img);
    }

    @Override
    public void onUserInfoSuccess(MyUserInfoBean bean) {
        if (refreshLayout != null)
            refreshLayout.finishRefresh();
        myUserInfoBean = bean;
        GlideUtil.ImageLoad(getMe(), bean.getResult().getHead(), imgUserHead);
        tvUserName.setText(bean.getResult().getNickname());
        tvUserCode.setText(bean.getResult().getInvite());
        tvUserStock.setText(bean.getResult().getShares() + "股");
        tvNumCardroll.setText(String.valueOf(bean.getResult().getCoupon()));
        tvNumFans.setText(String.valueOf(bean.getResult().getFans()));
        tvNumProfit.setText(bean.getResult().getMoney());
        tvUserPhone.setText(bean.getResult().getTel());
        if (bean.getResult().getNotify_count() > 0) {
            tvMsg.setVisibility(View.VISIBLE);
            tvMsg.setText(String.valueOf(bean.getResult().getNotify_count()));
        } else tvMsg.setVisibility(View.GONE);
    }
}
